<template>
  <div class="box">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>用户信息</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="text item">
        <!-- 完善用户信息表单 -->
        <el-form
          :model="userInfoForm"
          :rules="rules"
          ref="userInfoForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <!-- 编号-->
          <el-form-item label="编号" prop="userid">
            <span>{{ userInfoForm.userid }}</span>
          </el-form-item>
          <!-- 头像 -->
          <el-form-item label="头像" prop="imgurl">
            <div class="flex j-l a-c">
              <el-avatar
                fit="contain"
                size="large"
                shape="circle"
                :src="userPicPath"
              ></el-avatar
              >&nbsp;&nbsp;&nbsp;
              <a href="/index/admin-modifyAvatar">设置</a>
            </div>
          </el-form-item>
          <!-- 用户名 -->
          <el-form-item label="昵称" prop="username">
            <span>{{ userInfoForm.username }}</span
            >&nbsp;&nbsp;<a :href="`/index/admin-modifypwd`">修改密码</a>
          </el-form-item>
          <!-- 手机 -->
          <el-form-item label="手机" prop="phone">
            <el-input v-model="userInfoForm.phone"></el-input>
          </el-form-item>
          <!-- 邮箱 -->
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userInfoForm.email"></el-input>
          </el-form-item>
          <!-- 个人介绍 -->
          <el-form-item label="个人简介">
            <el-input
              type="textarea"
              :rows="5"
              v-model="userInfoForm.desc"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('userInfoForm')"
              >修改</el-button
            >
            <el-button @click="resetForm('userInfoForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
//引入用户相关接口
import { getUserByName, completeAdmin } from "@/api/admin.js";
export default {
  data() {
    return {
      //用户头像路径
      userPicPath: "",
      //用户信息对象
      userInfoForm: {
        userid: "",
        username: "",
        password: "",
        imgurl: "",
        email: "",
        desc: "",
        phone: "",
      },
      // 规则
      rules: {
        phone: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            pattern: /^1[35789]\d{9}$/,
            message: "手机格式不正确",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/,
            message: "邮箱格式不正确",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //自定义方法
  methods: {
    //获取当前用户基本信息（根据用户昵称）
    getUser() {
      //问：当前用户名称是什么
      let username = localStorage.getItem("logname");
      getUserByName(username)
        .then((r) => {
          console.log(r);
          //将服务器响应的结果赋值给当前状态值currUser
          this.userInfoForm = r.data;
          //将图片路径传递给状态值保存
          this.userPicPath = "http://localhost:8080/elemrg" + r.data.imgurl;
        })
        .catch((e) => {
          console.log(e);
        });
    },
    //修改提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证成功
          this.$message({
            message: "验证成功",
            type: "success",
          });
          //发起完善信息的请求
          completeAdmin(this.userInfoForm)
            .then((r) => {
              console.log(r);
              //修改成功提示消息
              if (r.data.code === 0) {
                this.$message({
                  message: "完善用户信息完成",
                  type: "success",
                });
                //是否跳转
                //刷新页面
                this.getUser();
              } else {
                this.$message.error("完善信息失败");
                this.getUser();
              }
            })
            .catch((e) => {
              console.log(e);
              this.$message.error("完善信息请求服务器异常");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置数据
    resetForm(formName) {
      //调用回显的方法
      this.getUser();
    },
  },
  //钩子函数
  created() {
    //页面创建加载当前用户信息
    this.getUser();
  },
};
</script>

<style scoped>
.el-input,
.el-textarea {
  display: inline-block;
  width: 350px;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}
</style>
